#region Using References

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Beaker.OpenCube.Game;

#endregion

namespace Beaker.OpenCube.Textures
{
	public static class BlockTextureMapping
	{
		/// <summary>
		/// Maps the BlockTypes to file names
		/// </summary>
		private static readonly Dictionary<Block, IReadOnlyCollection<Tuple<BlockTextureFlags, string>>> TypeToNameMapping;


		/// <summary>
		/// Method that converts a list of textures to a readonly list
		/// </summary>
		/// <param name="textureMaps">The list of texture to convert to a readonly list</param>
		/// <returns></returns>
		private static IReadOnlyCollection<Tuple<BlockTextureFlags, string>> Textures(
			params Tuple<BlockTextureFlags, string>[] textureMaps )
		{
			return new ReadOnlyCollection<Tuple<BlockTextureFlags, string>>(
				textureMaps
			);
		}

		/// <summary>
		/// Method that converts the single name to a texture used for a whole block
		/// </summary>
		/// <param name="fileName">The name of the texture</param>
		/// <returns></returns>
		private static IReadOnlyCollection<Tuple<BlockTextureFlags, string>> Texture( string fileName )
		{
			return
				new
					ReadOnlyCollection<Tuple<BlockTextureFlags, string>>(
						new[]
						{
							Tuple.Create( BlockTextureFlags.AllFaces, fileName )
						}
					);
		}

		static BlockTextureMapping()
		{
			TypeToNameMapping =
				new Dictionary<Block, IReadOnlyCollection<Tuple<BlockTextureFlags, string>>>
				{
					{ Block.Stone, Texture( "stone" ) },
					{ Block.Grass, Textures(
						Tuple.Create( BlockTextureFlags.Side, "grass_side" ),
						Tuple.Create( BlockTextureFlags.SideOverlay, "grass_side_overlay" ),
						Tuple.Create( BlockTextureFlags.TopOverlay, "grass_top" ),
						Tuple.Create( BlockTextureFlags.Bottom, "dirt" )
					) },
					{ Block.Dirt, Texture("dirt") },
					{ Block.Cobblestone, Texture("stonebrick") },
					{ Block.WoodPlanksOak, Texture("wood" )},
					{ Block.WoodPlanksSpruce, Texture("wood_spruce" )},
					{ Block.WoodPlanksBirch, Texture("wood_birch" )},
					{ Block.WoodPlanksJungle, Texture("wood_jungle" )},
					{ Block.SaplingOak, Texture("sapling" )},
					{ Block.SaplingSpruce, Texture("sapling_spruce" )},
					{ Block.SaplingBirch, Texture("sapling_birch" )},
					{ Block.SaplingJungle, Texture("sapling_jungle" )},
					{ Block.Bedrock, Texture( "bedrock" )},
					{ Block.Water, Texture("water_flow") },
					{ Block.StationaryWater, Texture("water") },
					{ Block.Lava, Texture("lava_flow") },
					{ Block.StationaryLava, Texture("lava") },
					{ Block.Sand, Texture("sand") },
					{ Block.Gravel, Texture("gravel") },
					{ Block.GoldOre, Texture("oreGold") },
					{ Block.IronOre, Texture("oreIron"  ) },
					{ Block.CoalOre, Texture("oreCoal") },
					{ Block.WoodOak, Textures(
						Tuple.Create( BlockTextureFlags.Top | BlockTextureFlags.Bottom,"tree_top"),
						Tuple.Create( BlockTextureFlags.Side,"tree_side")
					) },
					{ Block.WoodSpruce, Textures(
						Tuple.Create( BlockTextureFlags.Top | BlockTextureFlags.Bottom,"tree_top"),
						Tuple.Create( BlockTextureFlags.Side,"tree_spruce")
					) },
					{ Block.WoodBirch, Textures(
						Tuple.Create( BlockTextureFlags.Top | BlockTextureFlags.Bottom,"tree_top"),
						Tuple.Create( BlockTextureFlags.Side,"tree_birch")
					) },
					{ Block.WoodJungle, Textures(
						Tuple.Create( BlockTextureFlags.Top | BlockTextureFlags.Bottom,"tree_top"),
						Tuple.Create( BlockTextureFlags.Side,"tree_jungle")
					) },
					{ Block.LeavesOak, Texture("leaves") },
					{ Block.LeavesSpruce, Texture("leaves_spruce") },
					{ Block.LeavesBirch, Texture("leaves_birch") },
					{ Block.LeavesJungle, Texture("leaves_jungle") },
					{ Block.Sponge, Texture("sponge") },
					{ Block.Glass, Texture("glass") },
					{ Block.LapisLazuliOre, Texture("oreLapis") },
					{ Block.LapisLazuliBlock, Texture("blockLapis") },
//					{ Block.Dispenser, "" },
//					{ Block.Sandstone, "" },
//					{ Block.NoteBlock, "" },
//					{ Block.Bed, "" },
//					{ Block.PoweredRail, "" },
//					{ Block.DetectorRail, "" },
//					{ Block.StickyPistion, "" },
//					{ Block.Cobweb, "" },
//					{ Block.TallGrass, "" },
//					{ Block.DeadBush, "" },
//					{ Block.Piston, "" },
//					{ Block.PistonExtension, "" },
//					{ Block.Wool, "" },
//					{ Block.BlockMovedByPistion, "" },
//					{ Block.Dandelion, "" },
//					{ Block.Rose, "" },
//					{ Block.BrownMushroom, "" },
//					{ Block.RedMushroom, "" },
//					{ Block.GoldBlock, "" },
//					{ Block.IronBlock, "" },
//					{ Block.DoubleSlabs, "" },
//					{ Block.Slab, "" },
//					{ Block.Bricks, "" },
//					{ Block.Tnt, "" },
//					{ Block.Bookshelf, "" },
//					{ Block.MossyCobblestone, "" },
//					{ Block.Obsidian, "" },
//					{ Block.Torch, "" },
//					{ Block.Fire, "" },
//					{ Block.MonsterSpawner, "" },
//					{ Block.WoodenOakStairs, "" },
//					{ Block.Chest, "" },
//					{ Block.RedstoneWire, "" },
//					{ Block.DiamondOre, "" },
//					{ Block.DiamondBlock, "" },
//					{ Block.CraftingTable, "" },
//					{ Block.Wheat, "" },
//					{ Block.Farmland, "" },
//					{ Block.Furnace, "" },
//					{ Block.FurnaceBurning, "" },
//					{ Block.SignPost, "" },
//					{ Block.WoodenDoor, "" },
//					{ Block.Ladder, "" },
//					{ Block.Rail, "" },
//					{ Block.CobblestoneStairs, "" },
//					{ Block.WallSign, "" },
//					{ Block.Lever, "" },
//					{ Block.StonePressurePlate, "" },
//					{ Block.IronDoor, "" },
//					{ Block.WoodenPressurePlate, "" },
//					{ Block.RedstoneOre, "" },
//					{ Block.RedstoneOreGlowing, "" },
//					{ Block.RedstoneTorchInactive, "" },
//					{ Block.RedstoneTorchActive, "" },
//					{ Block.StoneButton, "" },
//					{ Block.SnowDeck, "" },
//					{ Block.IceBlock, "" },
//					{ Block.SnowBlock, "" },
//					{ Block.Cactus, "" },
//					{ Block.ClayBlock, "" },
//					{ Block.SugarCane, "" },
//					{ Block.Jukebox, "" },
//					{ Block.Fence, "" },
//					{ Block.Pumpkin, "" },
//					{ Block.Netherrack, "" },
//					{ Block.SoulSand, "" },
//					{ Block.GlowstoneBlock, "" },
//					{ Block.NetherPortal, "" },
//					{ Block.JackOLantern, "" },
//					{ Block.CakeBlock, "" },
//					{ Block.RedstoneRepeaterInactive, "" },
//					{ Block.RedstoneRepeaterActive, "" },
//					{ Block.LockedChest, "" },
//					{ Block.Trapdoor, "" },
//					{ Block.MonsterEggCobblestone, "stonebrick" },
//					{ Block.MonsterEggCracked, "stonebricksmooth_cracked" },
//					{ Block.MonsterEggStone, "stonebricksmooth" },
//					{ Block.StoneBricksNormal, "stonebricksmooth" },
////					{ Block.HugeBrownMushroom, "" },
////					{ Block.HugeRedMushroom, "" },
//					{ Block.IronBars, "" },
//					{ Block.GlassPane, "" },
//					{ Block.Melon, "" },
//					{ Block.PumpkinStem, "" },
//					{ Block.MelonStem, "" },
//					{ Block.Vines, "" },
//					{ Block.FenceGate, "" },
//					{ Block.BrickStairs, "" },
//					{ Block.StoneBrickStairs, "" },
//					{ Block.Mycelium, "" },
//					{ Block.LilyPad, "" },
//					{ Block.NetherBricks, "" },
//					{ Block.NetherBricksFence, "" },
//					{ Block.NetherBricksStairs, "" },
//					{ Block.NetherWart, "" },
//					{ Block.EnchantmentTable, "" },
//					{ Block.BrewingStand, "" },
//					{ Block.Cauldron, "" },
//					{ Block.EndPortal, "" },
//					{ Block.EndPortalBlock, "" },
//					{ Block.EndStone, "" },
//					{ Block.DragonEgg, "" },
//					{ Block.RedstoneLampInactive, "" },
//					{ Block.RedstoneLampActive, "" },
//					{ Block.DoubleWoodenSlabsOak, "wood" },
//					{ Block.DoubleWoodenSlabsSpruce, "wood_spruce" },
//					{ Block.DoubleWoodenSlabsBirch, "wood_birch" },
//					{ Block.DoubleWoodenSlabsJungle, "wood_jungle" },
//					{ Block.WoodenSlabOak, "wood" },
//					{ Block.WoodenSlabSpruce, "wood_spruce" },
//					{ Block.WoodenSlabBirch, "wood_birch" },
//					{ Block.WoodenSlabJungle, "wood_jungle" },
//					{ Block.CocoaPod, "" },
//					{ Block.SandstoneStairs, "" },
//					{ Block.EmeraldOre, "" },
//					{ Block.EnderChest, "" },
//					{ Block.TripwireHook, "" },
//					{ Block.Tripwire, "" },
//					{ Block.EmeraldBlock, "" },
//					{ Block.SpruceWoodStairs, "wood_birch" },
//					{ Block.BirchWoodStairs, "wood_birch" },
//					{ Block.JungleWoodStairs, "wood_jungle" },
//					{ Block.CommandBlock, "commandBlock" },
//					{ Block.Beacon, "" },
//					{ Block.CobblestoneWallNormal, "stone" },
//					{ Block.CobblestoneWallMossy, "stoneMoss" },
//					{ Block.FlowerPot, "flowerPot" },
//					{ Block.Carrots, "" },
//					{ Block.Potatoes, "" },
//					{ Block.WoodenButton, "" },
//					{ Block.MobHead, "" },
//					{ Block.Anvil, "" },
//					{ Block.TrappedChest, "" },
//					{ Block.WeightedPressurePlateLight, "" },
//					{ Block.WeightedPressurePlateHeavy, "" },
//					{ Block.RedstoneComparatorInactive, "" },
//					{ Block.RedstoneComparatorActive, "" },
//					{ Block.DaylightSensor, "" },
//					{ Block.RedstoneBlock, "" },
//					{ Block.NetherQuartzOre, "" },
//					{ Block.Hopper, "" },
//					{ Block.QuartzBlock, "" },
//					{ Block.QuartzStairs, "" },
//					{ Block.ActivatorRail, "activatorRail" },
//					{ Block.Dropper, "" },
				};

			//NameToTypeMapping = new Dictionary<string, BlockType>();
			//foreach( KeyValuePair<BlockType, string> entry in TypeToNameMapping )
			//	NameToTypeMapping.Add( entry.Value, entry.Key );
		}

		public static IReadOnlyCollection<Tuple<BlockTextureFlags, string>> GetTextureMapping( Block block )
		{
			IReadOnlyCollection<Tuple<BlockTextureFlags, string>> mapping;
			TypeToNameMapping.TryGetValue( block, out mapping );

			return mapping;
		}

		public static IEnumerable<Block> GetSupportedBlockTypes()
		{
			return TypeToNameMapping.Keys;
		}
	}
}